Skip to content

Apache Maven dev note

Create a multi-module project

  1. Create the main project starting from a folder and convert it into a maven project as defined from the defined inside.

  2. define the packaging as 'pom' and define the two project as module: xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>it.andzac.dvlp</groupId> <artifactId>mproject</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging> <modules> <module>engine</module> <module>rpc</module> </modules> </project>

  3. Create the two sub-module project and define in their pom the relation with the defined parent (for the subproject is not necessary define group and version because it is inherit from the parent):

xml <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>it.andzac.dvlp</groupId> <artifactId>mproject</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>engine</artifactId> <name>engine</name> <!-- ...... --> </project> 4. If a module need a dependency with another module of the main (parent) project, is necessary add it as dependency in the dependencies section (In the example below engine need a dependency of rpc module and the pom listed below is relative to engine):

xml <!-- ...... --> <dependency> <groupId>com.ca.lsp</groupId> <artifactId>rpc</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency>

  1. In order to make the jar executable (with the necessary dependencies inside it) is necessary define the build section:

xml <!-- ...... --> <build> <plugins> <plugin> <artifactId>maven-assembly-plugin</artifactId> <version>3.1.0</version> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <mainClass>it.andzac.mproject.engine.App</mainClass> </manifest> </archive> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> </plugins> </build>

Note

In the <archive> tag is defined the main class that will be invoked when the jar will be launched.

  1. Now is possible invoke the maven command to create the jar that can be executed directly from the shell:

sh mvn clean install -DskipTests mvn package java -jar engine-0.0.1-SNAPSHOT-jar-with-dependencies.jar